//
//  AppDelegate.m
//  mingpianguanli
//
//  Created by ryou on 2021/6/23.
//

#import "AppDelegate.h"
#import "MPGLHomeViewController.h"
#import "ViewController.h"
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/AdSupport.h>
#import "BUDAnimationTool.h"
@interface AppDelegate ()<BUSplashAdDelegate,BUSplashZoomOutViewDelegate>
@property(nonatomic,strong)BUSplashAdView *mingPianSplash;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [UMConfigure initWithAppkey:@"60dd82948a102159db8492da" channel:@"app"];
  

    self.window =[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [[ViewController alloc]init];
    [self.window makeKeyAndVisible];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mpglconncheckNetworkState) name:kReachabilityChangedNotification object:nil];
    Reachability * mpglconn = [Reachability reachabilityForInternetConnection];
    [mpglconn startNotifier];
    [self mpglconncheckNetworkState];
    
    return YES;
}

- (void)mpglconncheckNetworkState
{
    Reachability *mpglconn = [Reachability reachabilityForInternetConnection];
    
    if ([mpglconn currentReachabilityStatus] != NotReachable) {

        [self  mingpianguanlirequestIDFA];
        
    } else { // 没有网络
    }
}
- (void)mingpianguanlirequestIDFA {
    if (@available(iOS 14, *)) {
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            // Tracking authorization completed. Start loading ads here.
            dispatch_async(dispatch_get_main_queue(), ^{
                [self  setupBUAdSDK];
            });

        }];
    } else {
        // Fallback on earlier versions
        [self  setupBUAdSDK];

    }
}

-(void)showView{
    UINavigationController *navc=[[UINavigationController alloc]initWithRootViewController:[[MPGLHomeViewController alloc]init]];
    self.window =[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = navc;
    [self.window makeKeyAndVisible];
}

- (void)setupBUAdSDK {

 
    
    NSInteger territory = [[NSUserDefaults standardUserDefaults]integerForKey:@"territory"];
    BOOL isNoCN = (territory>0&&territory!=BUAdSDKTerritory_CN);
    
    BUAdSDKConfiguration *configuration = [BUAdSDKConfiguration configuration];
    configuration.territory = isNoCN?BUAdSDKTerritory_NO_CN:BUAdSDKTerritory_CN;
    configuration.GDPR = @(0);
    configuration.coppa = @(0);
    configuration.CCPA = @(1);
  
    configuration.appID = kbuakey;
    [BUAdSDKManager startWithAsyncCompletionHandler:^(BOOL success, NSError *error) {

        dispatch_async(dispatch_get_main_queue(), ^{
                // splash AD demo
                [self addSplashAD];
              });

    }];
  
}

#pragma mark - Splash
- (void)addSplashAD {
    CGRect frame = [UIScreen mainScreen].bounds;
    self.mingPianSplash = [[BUSplashAdView alloc] initWithSlotID:kkaipingkey frame:frame];
    // tolerateTimeout = CGFLOAT_MAX , The conversion time to milliseconds will be equal to 0
    self.mingPianSplash.tolerateTimeout = 3;
    // 不支持中途更改代理，中途更改代理会导致接收不到广告相关回调，如若存在中途更改代理场景，需自行处理相关逻辑，确保广告相关回调正常执行。
    self.mingPianSplash.delegate = self;

    UIWindow *keyWindow = self.window;

    [self.mingPianSplash loadAdData];
    [keyWindow.rootViewController.view addSubview:self.mingPianSplash];
    self.mingPianSplash.rootViewController = keyWindow.rootViewController;
}
-(void)applicationWillEnterForeground:(UIApplication *)application{
    [self addSplashAD];
}
- (void)removeSplashAdView {
    if (self.mingPianSplash) {
        [self.mingPianSplash removeFromSuperview];
        self.mingPianSplash = nil;
    }
    [self showView];

}


- (void)splashAdDidLoad:(BUSplashAdView *)splashAd {
    [self pbu_logWithSEL:_cmd msg:[NSString stringWithFormat:@"mediaExt %@",splashAd.mediaExt]];
    if (splashAd.zoomOutView) {
        UIViewController *parentVC = [UIApplication sharedApplication].keyWindow.rootViewController;
        //Add this view to your container
        [parentVC.view insertSubview:splashAd.zoomOutView belowSubview:splashAd];
        splashAd.zoomOutView.rootViewController = parentVC;
        splashAd.zoomOutView.delegate = self;
    }
}

- (void)splashAdDidClose:(BUSplashAdView *)splashAd {
    if (splashAd.zoomOutView) {
        __weak typeof(splashAd) weakSplashAdView = splashAd;
        [[BUDAnimationTool sharedInstance] transitionFromView:splashAd toView:splashAd.zoomOutView splashCompletion:^{
            [weakSplashAdView removeFromSuperview];
        }];
    } else{
        // Be careful not to say 'self.mingPianSplash = nil' here.
        // Subsequent agent callbacks will not be triggered after the 'splashAdView' is released early.
        [splashAd removeFromSuperview];
    }
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashAdDidClick:(BUSplashAdView *)splashAd {
    if (splashAd.zoomOutView) {
        [splashAd.zoomOutView removeFromSuperview];
    }
    // Be careful not to say 'self.mingPianSplash = nil' here.
    // Subsequent agent callbacks will not be triggered after the 'splashAdView' is released early.
    [splashAd removeFromSuperview];
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashAdDidClickSkip:(BUSplashAdView *)splashAd {
    if (splashAd.zoomOutView) {
        __weak typeof(self) weaSelf = self;
        [[BUDAnimationTool sharedInstance] transitionFromView:splashAd toView:splashAd.zoomOutView splashCompletion:^{
            [weaSelf removeSplashAdView];
        }];
    } else{
        // Click Skip, there is no subsequent operation, completely remove 'splashAdView', avoid memory leak
        [self removeSplashAdView];
    }
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashAd:(BUSplashAdView *)splashAd didFailWithError:(NSError *)error {
    // Display fails, completely remove 'splashAdView', avoid memory leak
    [self removeSplashAdView];
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashAdWillVisible:(BUSplashAdView *)splashAd {
    [self pbu_logWithSEL:_cmd msg:@""];
}





- (void)splashAdWillClose:(BUSplashAdView *)splashAd {
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashAdDidCloseOtherController:(BUSplashAdView *)splashAd interactionType:(BUInteractionType)interactionType {
    // No further action after closing the other Controllers, completely remove the 'splashAdView' and avoid memory leaks
    [self removeSplashAdView];
    
    [self pbu_logWithSEL:_cmd msg:@""];
}



- (void)splashAdCountdownToZero:(BUSplashAdView *)splashAd {
    // When the countdown is over, it is equivalent to clicking Skip to completely remove 'splashAdView' and avoid memory leak
    if (!splashAd.zoomOutView) {
        [self removeSplashAdView];
    }
    [self pbu_logWithSEL:_cmd msg:@""];
}

#pragma mark - BUSplashZoomOutViewDelegate
- (void)splashZoomOutViewAdDidClick:(BUSplashZoomOutView *)splashAd {
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashZoomOutViewAdDidClose:(BUSplashZoomOutView *)splashAd {
    // Click close, completely remove 'splashAdView', avoid memory leak
    [self removeSplashAdView];
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashZoomOutViewAdDidAutoDimiss:(BUSplashZoomOutView *)splashAd {
    // Back down at the end of the countdown to completely remove the 'splashAdView' to avoid memory leaks
    [self removeSplashAdView];
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashZoomOutViewAdDidCloseOtherController:(BUSplashZoomOutView *)splashAd interactionType:(BUInteractionType)interactionType {
    // No further action after closing the other Controllers, completely remove the 'splashAdView' and avoid memory leaks
    [self removeSplashAdView];
    [self pbu_logWithSEL:_cmd msg:@""];
}


- (void)pbu_logWithSEL:(SEL)sel msg:(NSString *)msg {

}

@end
